package com.wn.cloud.seata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.cloud.seata.api.AccountService;
import com.wn.cloud.seata.api.StorageService;
import com.wn.cloud.seata.entity.OrderEntity;
import com.wn.cloud.seata.mapper.OrderMapper;
import com.wn.cloud.seata.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @FileName: OrderServiceImpl
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/4/17 - 16:14
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {

    @Autowired
    private StorageService storageService;

    @Autowired
    private AccountService accountService;

    /**
     * 支付过程
     *
     * @param order 订单实体
     */
    @Override
    public void create(OrderEntity order) {
        // 创建订单
        log.info("----------->开始创建订单");
        this.save(order);

        // 减少库存
        log.info("----------->开始减少库存");
        storageService.decreaseStorage(order.getProductId(), order.getCount());

        // 减少账户金额
        log.info("----------->开始减少账户金额");
        accountService.decreaseAccount(order.getUserId(), order.getMoney());

        // 修改订单状态为已完成
        log.info("----------->开始修改订单状态");
        OrderEntity orderEntity = new OrderEntity().setId(order.getId()).setStatus(1);
        this.updateById(orderEntity);

    }
}
